{extend name="platform/base" /}
{block name="resources"/}
<style>
.fa {display: grid;}
.text-active, .active>.text {display: none;}
.searchable-select-item.selected ,.searchable-select-item.hover {background: #65bd77!important;}
.searchable-select{line-height: 1.2!important; border-color: #d9d9d9!important;}
.searchable-select-dropdown{border: 1px solid #d9d9d9 !important;}
.searchable-select-holder{
	border: 1px solid #d9d9d9 !important;
	-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0) !important;;
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0) !important;;
    -webkit-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}
.searchable-select-holder{border-radius: 2px!important;}

</style>
{/block}
 {block name="main"}
<div class="row padder-v">
	<div class="col-sm-5">
		<!-- 			<a href="PLATFORM_MAIN/Auth/addUser" class="btn btn-sm btn-primary"> -->
		<!-- 			<i class="fa fa-plus"></i> 添加用户 </a> -->
	</div>
	
	<div class="col-sm-2 js-shop-block" >
		
			<select id="shop_id" class="input-sm form-control" >
				<option value="">请选择店铺名称</option>
				{foreach name="shoplist" item ="vo"}
				<option value="{$vo.shop_id}">{$vo.shop_name}</option>
				{/foreach}
			</select>
		
	</div>
	
	<div class="col-sm-2">
		<input type="text" id="search_text" class="input-sm form-control" placeholder="请输入商品名称"> 
	</div>
	
	<div class="col-sm-2 js-shop-block" >
		<select class="input-sm form-control" id="shop_type" >
			<option value="">请选择商品状态</option>
            <option value="">全部</option>
            <option value="-1">待审核</option>
			<option value="1">上架</option>
			<option value="0">下架</option>
       </select>	
	</div>
	
	<div class="col-sm-1">
		<button class="btn btn-sm btn-default" type="button" onclick="LoadingInfo(1)">搜索</button>
	</div>
</div>
<section class="panel panel-default">
	<div class="table-responsive">
		<table class="table table-striped b-light text-sm">
			<colgroup>
				<col width="3%">
				<col width="5%">
				<col width="5%">
				<col width="18%">
				<col width="10%">
				<col width="5%">
				<col width="5%">
				<col width="5%">
				<col width="8%">
				<col width="8%">
				<col width="8%">
				<col width="7%">
				<col width="13%">
			</colgroup>
			<thead>
				<tr>
					<th><input type="checkbox"></th>
					<th>图片</th>
					<th>编号</th>
					<th class="left">商品名称</th>
					<th>店铺</th>
					<th class="center">新品</th>
					<th class="center">精品</th>
					<th class="center">热卖</th>
					<th>分类&品牌</th>
					<th class="right">价格</th>
					<th class="right">库存</th>
					<th class="center">商品状态</th>
					<th class="center">操作</th>
				</tr>
			</thead>
			<tbody id="list">
				
			</tbody>
		</table>
	</div>
	
	<!-- 违规下架添加备注 开始 -->
	<div class="modal fade" id="OffShelfReason">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">违规下架备注信息</h4>
				</div>
				
				<div class="modal-body">
					<form class="form-horizontal">
						<div>
							<lable style="float: left;margin-right: 20px;line-height: 35px;font-weight: 600;">备注：</lable>
							<textarea id="off_shelf_reason" type="text"	style="width: 400px; height: 80px; border-color: #d9d9d9; border-radius: 2px;"></textarea>
							<input type="hidden" id="goods_id" value="" />
						</div>
					</form>
				</div>
				
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-info" onclick="modifyGoodsOnline(0,1)">确定</button>
				</div>
			</div>
		</div>
	</div>	
	
	{include file="platform/page" /}
</section>
{/block} 
{block name="script"}
<link rel="stylesheet" type="text/css" href="__ROOT__/public/admin/css/plugin/jquery.searchableSelect.css">
<script src="__ROOT__/public/admin/js/plugin/jquery.searchableSelect.js"></script>
<script src="__STATIC__/js/BootstrapMenu.min.js"></script>

<script type="text/javascript">
function modifyGoodsRecommend(goods_id, type, is_recommend){
	$.ajax({
		type : "post",
		url : "{:__URL('PLATFORM_MAIN/Goods/modifyGoodsRecommend')}",
		async : true,
		data : {
			"goods_id" : goods_id,
			"type" : type,
			"is_recommend" : is_recommend
		},
		success : function(data) {
			if(data.code > 0){
				LoadingInfo(1);
			}
		}
	});
}



//当前可搜索的下拉选项框
var curr_searchable_select = null;
$(function(){

		//可搜索的店铺下拉选项框
// 		curr_searchable_select = $('#shop_id').searchableSelect();
// 		getGoodsShopList();

		//右键复制
// 		var menu = new BootstrapMenu('.searchable-select-holder', {
// 			actions: [{
// 			name: '复制',
// 			onClick: function() {
// 				var shop_name = $('#shop_id option:selected').text();
// 				$("#selected_shop_name").val(shop_name);
// 				var copy_content = document.getElementById('selected_shop_name');
// 				copy_content.select();
// 				document.execCommand("Copy");
// 				showTip("复制成功",'success');
// 			}
// 			}]	
// 		}); 

// 		$(".searchable-select-input").on("keyup",function(){
// 			if($(this).val().length>100){
// 				showTip("查询限制在100个字符以内","warning");
// 				return;
// 			}
// 			if($(this).attr("data-value") != $(this).val()){
// 				$(this).attr("data-value",$(this).val());
// 				getGoodsShopList($(this).val());
// 			}
// 		});
	LoadingInfo(1);
});

//查询商品品牌列表
// function getGoodsShopList(search_text){
// 	var page_index = 1;
// 	var page_size = 20;
// 	$.ajax({
// 		type : "post",
// 		url : "{:__URL('PLATFORM_MAIN/shop/shopList')}",
// 		data : { "page_index" : page_index, "page_size" : page_size, "search_text" : search_text },
// 		success : function(res){
// 			//console.log(res);
// 			var html = '<option value="">请选择店铺名称</option>';
// 			if(res.total_count>0){
				
// 				for(var i=0;i<res['data'].length;i++){
// 					html += '<option value="' + res['data'][i].shop_id + '">' + res['data'][i].shop_name + '</option>';
// 				}
// 			}
// 			$("#shop_id").html(html);
// 			//更新搜索结果
// 			$(".js-shop-block .searchable-select-items .searchable-select-item").remove();
// 			curr_searchable_select.buildItems();
// 		}
// 	});
// }

	//查询用户列表
	function LoadingInfo(pageIndex) {
	    var url = window.parent.location.href;
	    console.log(url);
        var shop_id = $("#shop_id").val();
		var goods_name =$("#search_text").val();
		var state = $("#shop_type").val();
		$.ajax({
			type : "post",
			url : url,
			async : true,
			data : {
				"pageIndex" : pageIndex, "shop_id" : shop_id, "goods_name" : goods_name, "state" : state
			},
			success : function(data) {
				var html = '';
				$("#total_count_num").text(data["total_count"]);
				$("#page_count_num").text(data["page_count"]);
				$("#page_count").val(data["page_count"]);
				$("#pageNumber a").remove();
				
				if (data["data"].length > 0) {
					for (var i = 0; i < data["data"].length; i++) {
						html += '<tr>';
						html += '<td><input type="checkbox" name="sub" value="'+ data['data'][i]['goods_id']+'"></td>';
						html += '<td><img src="{:__IMG($default_goods_img)}" class="lazy_load" data-original="'+__IMG(data["data"][i]["pic_cover_micro"])+'" /></td>';
						html += '<td>'+data['data'][i]['code']+'</td>';
						html += '<td>'+data["data"][i]["goods_name"]+'</td>';
						html += '<td>'+data["data"][i]["shop_name"]+'</td>';
						if(data["data"][i]["is_platform_new"] > 0){
							html += '<td class="center"><a href="#" class="active" onclick="modifyGoodsRecommend('+data["data"][i]["goods_id"]+',1,0)" data-toggle="class"><i class="fa fa-check text-success text-active"></i><i class="fa fa-times text-danger text"></i></a></td>';
						}else{
							html += '<td class="center"><a href="#" class="" onclick="modifyGoodsRecommend('+data["data"][i]["goods_id"]+',1,1)" data-toggle="class"><i class="fa fa-check text-success text-active"></i><i class="fa fa-times text-danger text"></i></a></td>';
						}
						if(data["data"][i]["is_platform_best"] > 0){
							html += '<td class="center"><a href="#" class="active" onclick="modifyGoodsRecommend('+data["data"][i]["goods_id"]+',2,0)" data-toggle="class"><i class="fa fa-check text-success text-active"></i><i class="fa fa-times text-danger text"></i></a></td>';
						}else{
							html += '<td class="center"><a href="#" class="" onclick="modifyGoodsRecommend('+data["data"][i]["goods_id"]+',2,1)" data-toggle="class"><i class="fa fa-check text-success text-active"></i><i class="fa fa-times text-danger text"></i></a></td>';
						}
						if(data["data"][i]["is_platform_hot"] > 0){
							html += '<td class="center"><a href="#" class="active" onclick="modifyGoodsRecommend('+data["data"][i]["goods_id"]+',3,0)" data-toggle="class"><i class="fa fa-check text-success text-active"></i><i class="fa fa-times text-danger text"></i></a></td>';
						}else{
							html += '<td class="center"><a href="#" class="" onclick="modifyGoodsRecommend('+data["data"][i]["goods_id"]+',3,1)" data-toggle="class"><i class="fa fa-check text-success text-active"></i><i class="fa fa-times text-danger text"></i></a></td>';
						}
						if(data["data"][i]["brand_name"] != null){
							html += '<td><div class="media-body"><div>'+data["data"][i]["category_name"]+'</div><small class="text-muted">'+data["data"][i]["brand_name"]+'</small></div></td>';
						}else
						if(data["data"][i]["category_name"] != null){
							html += '<td><div class="media-body"><div>'+data["data"][i]["category_name"]+'</div></div></td>';
						}else{
							html += '<td class="left"><div class="media-body"><div>--</div></div></td>';
						}
						html += '<td class="right">' + data["data"][i]["price"] + '</td>';
						html += '<td class="right">' + data["data"][i]["stock"] + '</td>';
						if(data["data"][i]["state"] == 1){
							html += '<td class="center">已上架</td>';
						}else if(data["data"][i]["state"] == 10 || data["data"][i]["state"] == 0){
							html += '<td class="center">已下架</td>';
						}else if(data["data"][i]["state"] == -1){
                            html += '<td class="center">待审核</td>';
                        }
						html += '<td class="center">' +
							'<a  href="'+__URL('PLATFORM_MAIN/goods/goodsinfo?goodsid=' + data["data"][i]["goods_id"])+'">编辑</a>  &nbsp;&nbsp;&nbsp;';
						if(data["data"][i]["state"] == 1){
							html += ' <a href="javascript:void(0);" onclick="modifyGoodsOnline('+data["data"][i]["goods_id"]+', 0)">违规下架</a>';
						}else if(data["data"][i]["state"] == 10 || data["data"][i]["state"] == 0){
							html += ' <a href="javascript:void(0);" onclick="deleteGoods('+data["data"][i]["goods_id"]+')">删除</a>&nbsp;&nbsp;&nbsp; ';
							html += ' <a href="javascript:void(0);" onclick="OffShelfReason('+data["data"][i]["goods_id"]+', this)" title="'+data["data"][i]["off_shelf_reason"]+'">下架原因</a> ';
						}else if(data["data"][i]["state"] == -1){
                            html += ' <a href="javascript:void(0);" onclick="modifyGoodsExam('+data["data"][i]["goods_id"]+')">审核</a>';
                        }
						
						html += '</td>';
						html += '</tr>';
					}
				} else {
					html += '<tr align="center"><td colspan="13">暂无商品数据记录</td></tr>';
				}
				$("#list").html(html);
				var totalpage = $("#page_count").val();
				if (totalpage == 1) {
					changeClass("all");
				}
				var $html = pagenumShow(jumpNumber,totalpage,{$pageshow})
				$("#pageNumber").append($html);
				img_lazyload();
			}
		});
	}
	
	
	//商品上下架添加备注
	function modifyGoodsOnline(goods_ids,status){
		$("#OffShelfReason").modal('show');
		if(goods_ids!=0){
			$("#goods_id").val(goods_ids);
		}
		var goods_ids = $("#goods_id").val();
		var off_shelf_reason = $("#off_shelf_reason").val();

		if(status == 1){
			$.ajax({
				type : "post",
				url : "{:__URL('PLATFORM_MAIN/goods/modifygoodsoffline')}",
				async : true,
				data : {
					"goods_ids" : goods_ids, "off_shelf_reason" : off_shelf_reason
				},
				success : function(data) {
					if(data["code"] > 0 ){
						showMessage('success', "下架成功","{:__URL('PLATFORM_MAIN/goods/goodslist')}");
					}else{
						showMessage('error', "下架失败");
					}
				}
			})
		}
	}
	//查看商品上下架原因 
	function OffShelfReason(goods_ids, event){
	
		$("#OffShelfReason").modal('show');
		var off_shelf_reason = $(event).attr('title');
		$("#goods_id").val(goods_ids);
		$("#off_shelf_reason").html(off_shelf_reason);
	}

    //商品审核
    function modifyGoodsExam(goods_ids){
        $( "#dialog" ).dialog({
            buttons: {
                "确定": function() {
                    $(this).dialog('close');
                    $.ajax({
                        type : "post",
                        url : "{:__URL('PLATFORM_MAIN/goods/modifygoodsexam')}",
                        async : true,
                        data : {
                            "goods_ids" : goods_ids
                        },
                        success : function(data) {
                            if(data["code"] > 0 ){
                                showMessage('success', "通过成功","{:__URL('PLATFORM_MAIN/goods/goodslist')}");
                            }else{
                                showMessage('error', "通过失败");
                            }
                        }
                    })
                },
                "取消,#e57373": function() {
                    $(this).dialog('close');
                }

            },
            contentText:"你确定通过此审核吗？",
            title:"消息提醒"
        });
    }
	
	function deleteGoods(goods_ids){
		$( "#dialog" ).dialog({
            buttons: {
                "确定": function() {
                    $(this).dialog('close');
                    $.ajax({
            			type : "post",
            			url : "{:__URL('PLATFORM_MAIN/goods/deletegoods')}",
            			async : true,
            			data : {
            				"goods_ids" : goods_ids
            			},
            			success : function(data) {
            				if(data["code"] > 0 ){
            					showMessage('success', "删除成功","{:__URL('PLATFORM_MAIN/goods/goodslist')}");
            				}else{
            					showMessage('error', "删除失败");
            				}
            			}
            		})
                },
                "取消,#e57373": function() {
                    $(this).dialog('close');
                }
                
            },
            contentText:"你确定删除吗？",
            title:"消息提醒"
        });
	}
	$("#search_text").keypress(function(e){
		if(e.keyCode == 13){
			LoadingInfo(1);
		}
	});
</script>
{/block}
